package com.ldl.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @author Administrator
 * Author : L.D.L
 * E-mail : 1419485293@qq.com
 * Date : 2020年8月6日下午7:55:23
 */
public class CharsetFilter implements Filter {
	
	private String charset = "UTF-8";
	private String contentType = "text/html;charset=UTF-8";
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		if(filterConfig.getInitParameter("charset")!=null){
			charset = filterConfig.getInitParameter("charset");
		}
		
		if(filterConfig.getInitParameter("contentType")!=null){
			contentType = filterConfig.getInitParameter("contentType");
		}
	}
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		if(request.getCharacterEncoding()==null){
			request.setCharacterEncoding(charset);
		}
		if(response.getContentType()==null){
			response.setContentType(contentType);			
		}
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
	}

}
